package com.dawnClass.service.impl;


import com.dawnClass.mapper.ConsumerMapper;
import com.dawnClass.pojo.Consumer;
import com.dawnClass.pojo.ConsumerExample;
import com.dawnClass.service.RegisterService;

import com.dawnClass.util.AESUtil;
import com.dawnClass.util.RSAUtils;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.List;


@Service
public class RegisterServiceImpl implements RegisterService {
    private static KeyPair keyPair;
    @Autowired
    private ConsumerMapper consumerMapper;


    @Override
    public ServiceResponse save(String consumerName , Long consumerTel , String consumerPassword) {
        System.out.println("注册密码："+consumerPassword);

        String encrypted = consumerPassword;
        String privateKey = getPrivateKey();

        try {
            System.out.println("RSA解密中.....");
            String decrypted = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));

            System.out.println("RSA解密后：" + decrypted);
            String encrypt =  AESUtil.encrypt(decrypted);

            System.out.println("AES加密后：" + encrypt);
            consumerPassword = encrypt;

        } catch (Exception e) {
            e.printStackTrace();
        }
        /*try {
            consumerPassword = RSAUtils.encrypt(consumerPassword,RSAUtils.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        System.out.println("consumerPassword:"+consumerPassword);
        Consumer consumer = new Consumer();
        consumer.setConsumerName(consumerName);
        consumer.setConsumerTel(consumerTel);
        consumer.setConsumerPassword(consumerPassword);
        int rows = consumerMapper.insert(consumer);
        if(rows>0)
            return ServiceResponse.saveSuccess(rows);
        return ServiceResponse.saveFail(null);
    }

    @Override
    public ServiceResponse regist(Long consumerTel) {
        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andConsumerTelEqualTo(consumerTel);


        List<Consumer> consumerList = consumerMapper.selectByExample(example);
        System.out.println("consumerList.size(): "+consumerList.size());
        if (consumerList.size() > 0) {
            return ServiceResponse.getSuccess("existence");
        }
        return ServiceResponse.getFail("non-existent");
//        return null;
        }

    @Override
    public ServiceResponse getPublicKey() {
        try {
            keyPair = RSAUtils.genKeyPair(1024);
            PublicKey publicKey=keyPair.getPublic();
            System.out.println("公钥：");
            String pubKey= new String(Base64.getEncoder().encode(publicKey.getEncoded()));
            System.out.println(pubKey);
            return ServiceResponse.getSuccess(pubKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResponse.getFail(null);
    }


    @Override
    public String getPrivateKey() {
        try {
            PrivateKey privateKey=keyPair.getPrivate();
            System.out.println("get私钥：");
            String priKey = new String(Base64.getEncoder().encode(privateKey.getEncoded()));
            System.out.println(priKey);
            return priKey;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
